<template>
  <a-modal :visible="props.show" class="ckd-dialog" :on-before-cancel="close">
    <template #title>
      <div class="ckd-dialog-title w-full h-[56px] flex justify-between items-center">{{ props.title }}</div>
    </template>
    <div class="content-box w-full h-auto overflow-hidden overflow-y-auto box-border">
      <a-form 
        :model="formData"
        :rules="UserRuls" 
        label-align="right" 
        :label-col-props="{span: 4}"
        ref="RefForm"
      >
        <a-form-item label="用户账号" field="account">
          <a-input v-model="formData.account" placeholder="请输入"></a-input>
        </a-form-item>
        <a-form-item label="账号状态" field="status">
          <a-switch v-model="(formData as any).status" :check-value="1" :uncheck-value="0" />
        </a-form-item>
        <a-form-item label="用户名称" field="username">
          <a-input v-model="formData.username" placeholder="请输入"></a-input>
        </a-form-item>
        <a-form-item label="所属组织" field="org_code">
          <a-cascader 
            :options="orgTree" 
            :field-names="{ value: 'code', label: 'org_name'}" 
            v-model="formData.org_code"  placeholder="请选择" 
            :check-strictly="true"
          />
        </a-form-item>
        <a-form-item label="联系电话" field="phone">
          <a-input v-model="formData.phone" placeholder="请输入"></a-input>
        </a-form-item>
        <a-form-item label="电子邮箱" field="email">
          <a-input v-model="formData.email" placeholder="请输入"></a-input>
        </a-form-item>
        <a-form-item label="输入密码" field="passwd" :required="!props.data">
          <a-input v-model="formData.passwd" placeholder="请输入"></a-input>
        </a-form-item>
      </a-form>
    </div>
    <template #footer>
      <a-button @click="close">取消</a-button>
      <a-button type="primary" @click="submit">确定</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
import { watch, ref, onMounted, nextTick, type PropType } from 'vue';
import * as Api from '@/api/user/useManagement';
import * as T from '@/api/user/userManagement_type';

const props = defineProps({
  show: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '新增账户'
  },
  data: {
    type: Object as PropType<any>,
    default: undefined
  }
})
const emits = defineEmits(['close', 'save', 'submit'])
const RefForm = ref()
const orgTree = ref<T.TOrgItem[]>([])

const formData = ref<T.TUserItem>(T.defaultUserItem());

const UserRuls:Record<string, any |any[]> = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  status: [
    { required: true, message: '请选择账号状态', trigger: 'change' }
  ],
  org_code: [
    { required: true, message: '请选择机构', trigger: 'change' }
  ],
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式错误', trigger: 'blur' }
  ],
  passwd: [
    // 自定义验证
    { validator: (value: string, callback: (arg0: any) => void) => {
      let message = ''
      if (props.data) return callback(message) // 编辑时不验证密码

      if(!value) {
        message = '请输入密码'
      }
      if(value.length < 6) {
        message = '密码长度不能小于6位'
      }
      if(value.length > 20) {
        message = '密码长度不能大于20位'
      }
      callback(message)
    }, trigger: 'blur' }
  ]
}

function submit() {
  if (!RefForm.value) return
  RefForm.value.validate().then((rs: any) => {
    if (!rs) {
      emits('submit', {...formData.value})
      close()
      return
    }
  })
}

function close() {
  emits('close')
  formData.value = { ...T.defaultUserItem() }
  return false
}

function getOrgTreeList() {
  Api.getOrgTreeList().then(res => {
    const {code, data, errMsg} = res
    if (code === 200 && data) {
      orgTree.value = [data]
      return
    }
    console.log(`[ 组织树 ]=>`, errMsg)
  })
}

watch(() => props.show, (val) => {
  if (val) {
    getOrgTreeList()

    if (!props.data) return
    const { account } = props.data
    Api.getUserInfo(account).then(res => {
      if (res.code === 200 && res.data) {
        formData.value = { ...res.data }
      }
    })
  }

}, {
  immediate: true
})



</script>

<style lang="less">
.ckd-dialog-title{
  font-family: Microsoft YaHei;
  font-size: 16px;
  font-weight: bold;
  color: #394D73;
}
.ckd-dialog{
  .arco-modal-wrapper{
    .arco-modal{
      width: 688px;
      .arco-modal-header{
        border-bottom: 0!important;
      }
      .arco-modal-footer{
        border-top: 0!important;
      }
    }
  }
}
</style>
<style lang="less" scoped>
.content-box{
  .arco-form{
    :deep(.arco-form-item-content) {
      box-sizing: border-box; padding-right: 200px;
    }
  }
}
</style>
